'''
Created on Oct 30, 2011

@author: oabalbin
'''

import os
import glob
import subprocess

def read_file(file,dir):
    '''
    #CHROM  POS     REF     ALT
    chr11   130286880       C       T
    '''
    ext='.mpileup.vcf'
    ifile= open(file)
    vcfs=vcf_files(dir,ext)
    ifile.next()
    print vcfs
    of=open(os.path.join(dir,'chron_snv_bug2.vcf'),'w')
    for l in ifile:
        f=l.strip('\r\n').split('\t')
        chr, pos, ref, alt = f[0],f[1],f[2],f[3]
        pt = chr+'$\'\\t\''+pos+'$\'\\t\''+'.'+'$\'\\t\''+ref+'$\'\\t\''+'[A;C;G;T];[A;C;G;T]'
        for v in vcfs:
            
            args=['grep',pt,v] #'%s$\'\t\'%s'%(chr,pos)
            args = ",".join(args).replace(',',' ')
            args =args.replace(';',',')
            
            r = subprocess.call(args,stdout=of, shell=True)
    of.close()


def check_bug(snp, vcf_file):
    pass

def vcf_files(dir,ext):
    return glob.glob(dir+"*"+ext)
    pass

file ='/exds/users/oabalbin/projects/exomes/nunez2/sam_calls_quick/156_followup_SNP_list.txt'
dir='/exds/users/oabalbin/projects/exomes/nunez2/sam_calls_quick/'
read_file(file,dir)
